﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;
using System.Data;
using System.Collections.ObjectModel;
using SmartMD.DataModel;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class PatientInfoDaoImpl : BaseGenericDaoImpl
    {
        public bool DeleteObject(Object entity)
        {
            PatientInfo eItem = (PatientInfo)entity;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" delete from DUO_PATIENT");
            sb.Append(" where PATIENTID ='" + eItem.Id + "'");

            this.dbConn.Delete(sb.ToString());
            return true;
        }
        public bool SaveOrUpdateObject(Object entity)
        {
            throw new NotImplementedException();
        }

        public bool savePatientInfos(ObservableCollection<PatientInfo> recordList)
        {
            PatientInfo patientInfo;
            bool isSaved = true;
          
            for (int i = 0; i < recordList.Count; i++)
            {
                patientInfo = recordList[i];
                bool success = SavePatientInfo(patientInfo);
               isSaved = isSaved & success;
            }
            return isSaved;
        }

        private bool SavePatientInfo(PatientInfo patientInfo)
        {
            if (patientInfo.Birthday == null && patientInfo.Age != null)
            {
                patientInfo.Birthday = GetBirthdayByAge(patientInfo.Age);
            }
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_PATIENT (PATIENTID, CLINICNO, NAME, SEX, AREA, BIRTHDAY, HOSPITALNO, FIRSTFOLLOWUP, TELEPHONE, ADDRESS, FOLLOWUPNO, PATHOLOGYNO, TELEPHONE2, EMAIL, HEIGHT, WEIGHT)");
            sb.Append(" values(");
            sb.Append(" '" + patientInfo.Id + "',");
            sb.Append(" '" + patientInfo.ClinicNo + "',");
            sb.Append(" '" + patientInfo.Name + "',");
            sb.Append(" '" + patientInfo.Sex + "',");
            sb.Append(" '" + patientInfo.Area + "',");
            sb.Append(" to_date ('" + patientInfo.Birthday.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ) ,");
            sb.Append(" '" + patientInfo.HospitalNo + "',");
            sb.Append(" to_date ('" + patientInfo.FirstFollowUpTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ) ,");
            sb.Append(" '" + patientInfo.Telephone + "',");
            sb.Append(" '" + patientInfo.Address + "',");
            sb.Append(" '" + patientInfo.FollowUpNo + "',");
            sb.Append(" '" + patientInfo.PathologyNo + "',");
            sb.Append(" '" + patientInfo.Telephone2 + "',");
            sb.Append(" '" + patientInfo.Email + "',");
            sb.Append(" '" + patientInfo.Height + "',");
            sb.Append(" '" + patientInfo.Weight + "'");
            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());
            return success;
        }

        private DateTime GetBirthdayByAge(int age)
        {
            DateTime date = System.DateTime.Now;
            date.AddYears(-age);
            return date;
        }

        public bool SaveOrUpdateObject(PatientInfo item)
        {
            bool success = true;
            PatientInfo tmpPatientInfo = null;
            if (item.ClinicNo != null)
            {
               tmpPatientInfo = GetPatientInfo("门诊号", item.ClinicNo);
            }
            else if (item.HospitalNo != null)
            {
               tmpPatientInfo = GetPatientInfo("住院号", item.HospitalNo);
            }
            else if (item.FollowUpNo != null)
            {
                tmpPatientInfo = GetPatientInfo("随访号", item.FollowUpNo);
            }

            if (tmpPatientInfo != null) //更新患者信息
            {
                if (item.FirstFollowUpTime != null)
                {
                    tmpPatientInfo.FirstFollowUpTime = item.FirstFollowUpTime;
                }
                if (item.FirstFollowUps != null)
                {
                    tmpPatientInfo.FirstFollowUps = item.FirstFollowUps;
                }
              
               success = SavePatientInfo(tmpPatientInfo);
            }
            else //保存新患者信息
            {
                success = SavePatientInfo(item);
            }
            return success;
        }

        public ObservableCollection<PatientInfo> loadAllPatientInfos()
        {
            ObservableCollection< PatientInfo> itemList = new ObservableCollection<PatientInfo>();
            PatientInfo patientInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_PATIENT");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    patientInfo = new PatientInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        patientInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    itemList.Add(patientInfo);
                }                
                return itemList;
            }
            else
                return null;      
        }

        public ObservableCollection<PatientInfo> GetPatientInfos(List<KVP> kvps)
        {
            if (kvps == null || kvps.Count <= 0)
                return null;
            ObservableCollection<PatientInfo> itemList = new ObservableCollection<PatientInfo>();
            PatientInfo patientInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_PATIENT t");
            for (int k = 0; k < kvps.Count; k++)
            {
                if (k == 0)
                {
                    sb.Append(" where ");
                }
                else {
                    sb.Append(" and ");
                }
                switch (kvps[k].key.Trim())
                {
                    case "姓名":
                        sb.Append(" t.NAME ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "门诊号":
                        sb.Append(" t.CLINICNO ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "住院号":
                        sb.Append(" t.HOSPITALNO ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "随访号":
                        sb.Append(" t.FOLLOWUPNO ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "地域":
                        sb.Append(" t.AREA ='" + kvps[k].value.Trim() + "'");
                        break;
                    case "疾病诊断":
                        sb.Append(" t.DIAGNOSE ='" + kvps[k].value.Trim() + "'");
                        break;
                    default:
                        return null;
                }
            }

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    patientInfo = new PatientInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        patientInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    itemList.Add(patientInfo);
                }
                return itemList;
            }
            else
                return null;      
        }
        public ObservableCollection<PatientInfo> loadAllPatientInfoByIds(List<string> ids)
        {
            if (ids == null || ids.Count <= 0)
                return null;
            ObservableCollection<PatientInfo> pInfoList = new ObservableCollection<PatientInfo>();
            PatientInfo pInfo = null;
            for (int i = 0; i < ids.Count; i++)
            {
               pInfo = loadPatientInfoById(ids[i]);
               pInfoList.Add(pInfo);
            }
            
            return pInfoList;
        }

        public PatientInfo loadPatientInfoById(string patientId)
        {
            PatientInfo patientInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_PATIENT");
            sb.Append(" where PATIENTID='" + patientId + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    patientInfo = new PatientInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        patientInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                }
                return patientInfo;
            }
            else
                return null;      
        }

        public PatientInfo GetPatientInfo(string key, string value)
        {
            PatientInfo patientInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_PATIENT");
            
            switch (key.Trim())
            {
                case "门诊号":
                    sb.Append(" where CLINICNO ='" + value.Trim() + "'");
                    break;
                case "住院号":
                    sb.Append(" where HOSPITALNO ='" + value.Trim() + "'");
                    break;
                case "随访号":
                    sb.Append(" where FOLLOWUPNO ='" + value.Trim() + "'");
                    break;
                case "姓名":
                    sb.Append(" where NAME ='" + value.Trim() + "'");
                    break;
                default:
                    return null;
            }

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    patientInfo = new PatientInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        patientInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                }
                return patientInfo;
            }
            else
                return null;      
        }
        public ObservableCollection<PatientInfo> GetPatientInfoByNames(string[] nameList)
        {
            ObservableCollection<PatientInfo> itemList = new ObservableCollection<PatientInfo>();
            PatientInfo patientInfo = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_PATIENT");
            for (int k = 0; k < nameList.Length; k++)
            {
                if (k == 0){
                    sb.Append(" where NAME = '" + nameList[k] + "'");
                }
                else {
                    sb.Append(" or NAME = '" + nameList[k] + "'");
                }
                
            }

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    patientInfo = new PatientInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        patientInfo.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    itemList.Add(patientInfo);
                }
                return itemList;
            }
            else
                return null;      
        }
     
    }
}
